import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
  direction: 'forward',
  gAlert: { show: false },
  gConfirm: { show: false },
  gToast: { show: false },
  gLoading: { show: false },
}

const getters = {
  gLoading: state => state.gLoading,
  gAlert: state => state.gAlert,
  gConfirm: state => state.gConfirm,
  gToast: state => state.gToast,
}


export default new Vuex.Store({
  state,
  getters,
  mutations: {
    UPDATE_DIRECTION (state, direction) {
      state.direction = direction
    },
    GLOBAL_ALERT (state, data) {
      if (data.show === false) {
        state.gAlert.show = false;
        return;
      }
      state.gAlert = data
    },
    GLOBAL_CONFIRM (state, data) {
      if (data.show === false) {
        state.gConfirm.show = false;
        return;
      }
      state.gConfirm = data
    },
    GLOBAL_LOADING (state, data) {
      if (data.show === false) {
        state.gLoading.show = false;
        return;
      }
      state.gLoading = data
    },
    GLOBAL_TOAST (state, data) {
      if (data.show === false) {
        state.gToast.show = false;
        return;
      }
      state.gToast = data
    }

  },
  actions:{
    //region 全局控件
    gAlert: ({commit}, data) => {
      commit('GLOBAL_ALERT', data)
    },
    gConfirm: ({commit}, data) => {
      commit('GLOBAL_CONFIRM', data)
    },
    gLoading: ({commit}, data) => {
      commit('GLOBAL_LOADING', data)
    },
    gToast: ({commit}, data) => {
      commit('GLOBAL_TOAST', data)
    }
    //endregion
  }
})
